package com.example.demo.demos.test;

import com.example.demo.demos.web.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@RequestMapping("/index1")
@RestController
public class HelloController {
    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
    @RequestMapping("/m1")
    public String m1(String name, Integer age){
        return "属性name = " + name + age;
    }
    @RequestMapping("/m2")
    public String m2( User user){
        return "属性user" + user.toString();
    }
    @RequestMapping("/m3")
    public String m3(String[] name){
        return "属性name" + Arrays.toString(name);
    }
    @RequestMapping("/m4")
    public String m4(@RequestParam List<String> list){
        return "属性list" + list + "，长度" + list.size();
    }
    @RequestMapping("/m5/{userId}/{name}")
    public String m5(@PathVariable Integer userId, @PathVariable String name){
        return "userId = " + userId + ",name = " + name;
    }
    @RequestMapping("/m6")
    public String m6(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("C:/Users/小天的电脑/Pictures/联想安卓照片/" + file.getOriginalFilename()));
        return "success";
    }
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        // 获取所有的cookie
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "获取cookie成功";
    }
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String name){
        return "cookie中value的值" + name;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("name","zhangsan");
        return "success";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            String name = (String) session.getAttribute("name");
            return "session" + name;
        }
        // String name = (String) session.getAttribute("name");
        return "session为空";
    }
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(required = false) String name){
        return "session:" + name;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent" + userAgent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "User-Agent" + userAgent;
    }
}
